package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-04-28
 * Time: 11:22
 */
/*
论坛首页测试
 */
public class HomePage extends Utils {
    public static String url = "http://127.0.0.1:58080/sign-in.html";
    public HomePage() {
        super(url);
    }

    /*
    未登录状态下 --- 访问论坛首页
    由 PageByNoLogin 进行测试
     */

    /*
    登录状态下 --- 测试论坛首页
     */
    // 检查页面(论坛首页)是否加载成功
    public void HomeLogin() throws InterruptedException {
        // 返回登录页并刷新页面
        driver.navigate().refresh();
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 加一个显式等待
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait.until(ExpectedConditions.urlToBe("http://127.0.0.1:58080/index.html"));
        // 获取首页 url
        String HomeUrl = driver.getCurrentUrl();
        // 断言判断是否跳转到了论坛首页
        assert HomeUrl.equals("http://127.0.0.1:58080/index.html");
    }

    // 搜索功能测试
    public void HomeSearch() throws InterruptedException {
        // 找到搜索框并输入想要搜索的内容
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")).sendKeys("1");
        // 点击搜索按钮(没有点击按钮, 选择从键盘输入 Enter 跳转)
//        driver.findElement(By.cssSelector("")).click();
        // 查找搜索框元素
        WebElement element = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input"));
        // 鼠标选中搜索框并点击
        Actions actions = new Actions(driver);
        actions.click(element).perform();
        // 键盘输入 Enter 进行搜索
//        actions.keyDown(Keys.ENTER).perform();
        // 因为 Enter 键一般不单独使用, 需要结合其他 Alt 按键使用, 所以修改代码
        actions.sendKeys(Keys.ENTER).build().perform();
        // 添加显式等待
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait.until(ExpectedConditions.urlToBe("http://127.0.0.1:58080/?"));
        // 获取当前页面的 url
        String searchUrl = driver.getCurrentUrl();
        // 断言检查是否搜索成功
        assert searchUrl.equals("http://127.0.0.1:58080/?");
    }

    //白天和夜间模式切换测试
    public void HomeColorSwitch() {
        // 找到模式切换按钮并点击, 即切换为夜间模式
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg")).click();
        // 添加显式等待
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait1.until(ExpectedConditions.urlToBe("http://127.0.0.1:58080/?theme=dark"));
        // 获取界面 url
        String blackUrl = driver.getCurrentUrl();
        // 断言检查是否切换成功
        assert blackUrl.equals("http://127.0.0.1:58080/?theme=dark");
        // 再次点击模式切换按钮, 即切换为白天模式
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light > svg")).click();
        // 添加显式等待
        WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait2.until(ExpectedConditions.urlToBe("http://127.0.0.1:58080/?theme=light"));
        // 获取界面 url
        String lighteUrl = driver.getCurrentUrl();
        // 断言判断是否切换成功
        assert lighteUrl.equals("http://127.0.0.1:58080/?theme=light");
    }

    // 接收站内消息通知测试
    public void HomeReceiveMessages() throws InterruptedException {
        // 点击消息查看按钮
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        // 添加一个强制等待
        Thread.sleep(1000);
        //断言检查是否正常打开
        String receive = driver.findElement(By.cssSelector("#offcanvasEndLabel")).getText();
        assert receive.equals("所有站内信");
    }

    // 回复私信测试
    public void HomeReplyMessage() throws InterruptedException {
        // 点击消息查看消息详情
        driver.findElement(By.cssSelector("#index_div_message_list > div > div > div:nth-child(3)")).click();
        // 点击回复按钮
        driver.findElement(By.cssSelector("#btn_index_message_reply")).click();
        // 这个输入框不能直接交互, 所以先点击之后再输入
        WebElement write = driver.findElement(By.cssSelector("#index_message_reply_receive_content"));
        Actions actions = new Actions(driver);
        actions.click(write).perform();
        // 输入回复内容
        driver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys("收到!");
        // 点击发送按钮
        driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
    }

    // 发送完之后关闭消息窗口
    public void HomeCloseMessage() {
        // 关闭查看消息页面
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
    }

    // 我的账户打开测试
    public void HomeMyAccountSuc() {
        // 点击头像打开
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        // 获取界面信息
        String ele1 = driver.findElement(By.cssSelector("#index_user_settings")).getText();
        // 断言检测
        ele1.equals("个人中心");
    }
    // 查看完之后关闭我的账户页面
    public void HomeCloseMyAccount() {
        // 关闭我的账户页面
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none")).click();
    }
    // 导航栏跳转查看详细帖子测试(以Java 板块为例)
    public void HomePostSkip() throws InterruptedException {
        // 点击 Java 板块
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a")).click();
        // 获取该页帖子标题信息
        Thread.sleep(1000);
        String content = driver.findElement(By.cssSelector("#artical-items-body > div > div > div.col > div.text-truncate > a > strong")).getText();
        // 断言检查是否正确跳转
        assert content.equals("3");
    }
}
